package com.ruoyi.bmxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.BmxtConstants;
import com.ruoyi.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bmxt.domain.BmSchoolLevel;
import com.ruoyi.bmxt.service.IBmSchoolLevelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校层次管理Controller
 * 
 * @author 魏建波
 * @date 2023-02-19
 */
@RestController
@RequestMapping("/bmxt/schoolLevel")
public class BmSchoolLevelController extends BaseController
{
    @Autowired
    private IBmSchoolLevelService bmSchoolLevelService;

    /**
     * 查询学校层次管理列表
     */
    @PreAuthorize("@ss.hasPermi('bmxt:schoolLevel:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmSchoolLevel bmSchoolLevel)
    {
        startPage();
        List<BmSchoolLevel> list = bmSchoolLevelService.selectBmSchoolLevelList(bmSchoolLevel);
        return getDataTable(list);
    }

    @GetMapping("/listAllSchoolLevel")
    public AjaxResult listAllSchoolLevel(BmSchoolLevel bmSchoolLevel)
    {
        List<BmSchoolLevel> list = bmSchoolLevelService.selectAllBmSchoolLevelList();
        return success(list);
    }

    /**
     * 导出学校层次管理列表
     */
    @PreAuthorize("@ss.hasPermi('bmxt:schoolLevel:export')")
    @Log(title = "学校层次管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmSchoolLevel bmSchoolLevel)
    {
        List<BmSchoolLevel> list = bmSchoolLevelService.selectBmSchoolLevelList(bmSchoolLevel);
        ExcelUtil<BmSchoolLevel> util = new ExcelUtil<BmSchoolLevel>(BmSchoolLevel.class);
        util.exportExcel(response, list, "学校层次管理数据");
    }

    /**
     * 获取学校层次管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('bmxt:schoolLevel:query')")
    @GetMapping(value = "/{schoolId}")
    public AjaxResult getInfo(@PathVariable("schoolId") Long schoolId)
    {
        return success(bmSchoolLevelService.selectBmSchoolLevelBySchoolLevelId(schoolId));
    }

    /**
     * 新增学校层次管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:schoolLevel:add')")
    @Log(title = "学校层次管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmSchoolLevel bmSchoolLevel)
    {
        // 验证一个学校和层次不能重复添加
        if (BmxtConstants.NOT_UNIQUE.equals(bmSchoolLevelService.checkSchoolLevelNameUnique(bmSchoolLevel)))
        {
            return error("添加失败，学校层次信息已存在");
        }
        return toAjax(bmSchoolLevelService.insertBmSchoolLevel(bmSchoolLevel));
    }

    /**
     * 修改学校层次管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:schoolLevel:edit')")
    @Log(title = "学校层次管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmSchoolLevel bmSchoolLevel)
    {
        // 验证一个学校和层次不能重复添加
        if (BmxtConstants.NOT_UNIQUE.equals(bmSchoolLevelService.checkSchoolLevelNameUnique(bmSchoolLevel)))
        {
            return error("修改失败，学校层次信息已存在");
        }
        return toAjax(bmSchoolLevelService.updateBmSchoolLevel(bmSchoolLevel));
    }

    /**
     * 删除学校层次管理
     */
    @PreAuthorize("@ss.hasPermi('bmxt:schoolLevel:remove')")
    @Log(title = "学校层次管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{schoolLevelId}")
    public AjaxResult remove(@PathVariable Long[] schoolLevelId)
    {
        return toAjax(bmSchoolLevelService.deleteBmSchoolLevelBySchoolIds(schoolLevelId));
    }
}
